package com.lingshi.shopping_manager_api.interceptor;

import cn.dev33.satoken.stp.StpInterface;
import cn.hutool.core.collection.CollUtil;
import com.lingshi.shopping.entity.Admin;
import com.lingshi.shopping.entity.Permission;
import com.lingshi.shopping.service.AdminService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StpInterfaceImpl implements StpInterface {

    @DubboReference
    private AdminService adminService;

    /**
     * 获取所有的权限 表达式（唯一资源地址），进行权限校验，推荐（精度比较细）
     * @param loginId  账号id 登录账号
     * @param loginType 账号类型
     * @return
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {

        List<Permission> allPermission = adminService.findAllPermission(loginId.toString());

        List<String> urls = allPermission.stream().map(Permission::getUrl).toList();
        // ["/goods/search","goods/insert",....]
        System.out.println("urls = " + urls);
        return CollUtil.isNotEmpty(urls) ? urls : List.of();
    }

    /**
     * 获取鄋的角色，进行角色校验
     * @param loginId  账号id
     * @param loginType 账号类型
     * @return
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        return List.of();
    }
}
